package com.mooc.yohoshop.ui.checkorder;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.CollectionOrderEntity;
import com.mooc.yohoshop.entity.CreateOrderEntity;
import com.mooc.yohoshop.entity.OrderDetailEntity;
import com.mooc.yohoshop.entity.OrderEntity;
import com.mooc.yohoshop.entity.PaySignEntity;
import com.mooc.yohoshop.filed.EventAction;
import com.mooc.yohoshop.ui.base.BaseActivity;
import com.mooc.yohoshop.ui.pay.AliPayActivity;
import com.mooc.yohoshop.widget.SecKillDivider;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.HashMap;
//vsyvtv1485@sandbox.com
//111111
public class CheckOrderActivity extends BaseActivity<CheckOrderRepo>implements View.OnClickListener {

    private TextView title_tv;
    private TextView pay_all_tv;
    private TextView order_title_tv;
    private RecyclerView orderlist_rv;
    private CreateOrderEntity orderEntity;
    private CheckOrderAdapter adapter;
    private float price = 0f;

    @Override
    protected int layout() {
        return R.layout.activity_checkorder;
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        f(R.id.back).setOnClickListener(this);
        f(R.id.pay_order_btn).setOnClickListener(this);
        title_tv = f(R.id.title_tv);
        pay_all_tv = f(R.id.pay_all_tv);
        orderlist_rv = f(R.id.orderlist_rv);
        order_title_tv = f(R.id.order_title_tv);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        orderlist_rv.setLayoutManager(manager);
        orderlist_rv.addItemDecoration(new SecKillDivider(this));
        adapter = new CheckOrderAdapter();
        orderlist_rv.setAdapter(adapter);
        title_tv.setText("确认订单");
        pay_all_tv.setText("总计:");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initData() {
        orderEntity = (CreateOrderEntity) getIntent().getSerializableExtra("value");
        HashMap<String,Object>map = new HashMap<>();
        map.put("order_id",orderEntity.data.get(0).order_id);
        request(repo.requestOrderValues(map));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshOrder(EventAction action){
        if (action.action.equals("9000")){
            Log.e("ZXY","9000");
            HashMap<String,Object>map=new HashMap<>();
            map.put("order_id",orderEntity.data.get(0).order_id);
            map.put("user_id",orderEntity.data.get(0).user_id);
            map.put("pay_id",orderEntity.data.get(0).pay_id);
            map.put("delivery_id",orderEntity.data.get(0).delivery_id);
            map.put("car_id",orderEntity.data.get(0).car_id);
            map.put("order_statues",1);
            map.put("ship_id",orderEntity.data.get(0).ship_id);
            request(repo.requestCheckOrder(map));
        }
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof OrderDetailEntity){
            OrderDetailEntity orderEntity = (OrderDetailEntity) baseEntity;
            order_title_tv.setText("订单编号:"+orderEntity.data.order_id);
            adapter.setNewInstance(orderEntity.data.order_goodsValues);
            for (int i = 0;i < orderEntity.data.order_goodsValues.size();i ++){
                OrderEntity.OrderGoodsValues values = orderEntity.data.order_goodsValues.get(i);
                int num = Integer.valueOf(values.shop_num);
                float p = Float.valueOf(values.shop_price);
                price += (p*num);
            }
            pay_all_tv.setText("总价:"+price);
        }else if (baseEntity instanceof PaySignEntity){
            PaySignEntity paySignEntity = (PaySignEntity) baseEntity;
            Bundle bundle = new Bundle();
            bundle.putString("alipayStr",paySignEntity.data);
            Intent intent = new Intent(this,AliPayActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }else if (baseEntity instanceof CollectionOrderEntity){
            if (baseEntity.message.equals("收款成功"))
                finish();
            else
                ToastUtils.showShort(baseEntity.message);
        }
    }

    @Override
    public void onClick(View v) {
        if (R.id.back == v.getId()){
            finish();
        }else if (R.id.pay_order_btn == v.getId()){
            String priceTv = ""+price;
            priceTv=priceTv.replace(".","");
            HashMap<String,Object>map = new HashMap<>();
            map.put("totalPrice",priceTv);
            map.put("orderId",orderEntity.data.get(0).order_id);
            request(repo.requestPaySign(map));
        }
    }
}
